package com.jack.vipmis.vip.api.web.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.jack.vipmis.common.web.controller.BaseController;
import com.jack.vipmis.vip.api.util.MsgUtil;
import com.jack.vipmis.vip.wei.entity.Circle;
import com.jack.vipmis.vip.wei.entity.Replytribune;
import com.jack.vipmis.vip.wei.service.CircleService;
import com.jack.vipmis.vip.wei.service.ReplyTribuneService;

/**
 * 回复微贴信息列表
 * 
 * @author JackLiang
 * @date 2014-7-7
 * @version 1.0
 */
@Controller
@RequestMapping(value = "api/replyTribune")
public class ReplyTribuneApiController extends BaseController<Replytribune, Long> {

	private static final String	DEFAULT_ENCONDING	= "utf-8";

	@Autowired
	private ReplyTribuneService	replyTribuneService;

	@Autowired
	private CircleService		circleService;

	/**
	 * 根据ID查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "find", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> find(@RequestParam("id") Long id) {
		try {
			Replytribune m = replyTribuneService.findByTribune(id);
			return MsgUtil.doneOk(m);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();

	}

	/**
	 * 该方法目前无效
	 * 
	 * @param tid
	 * @param input
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> addReplyTribune(@RequestParam("json") String json) {
		try {
			Replytribune m = JSON.parseObject(json, Replytribune.class);
			Replytribune rt = replyTribuneService.save(m);
			Circle circle = circleService.findOne(rt.getTribune());
			circle.setReplyId(rt.getId());
			circleService.update(circle);

			return MsgUtil.doneOk(rt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();

	}

	/**
	 * 
	 * @param tid
	 * @param input
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> saveReplyTribune(HttpServletRequest request) {
		try {
			String reContent = request.getParameter("reContent");
			String reUserName = request.getParameter("reUserName");
			String reUserAvatar = request.getParameter("userAvata");
			String reUserId = request.getParameter("reUserId");
			String reUserGender = request.getParameter("reUserGender");
			String tid = request.getParameter("tid");
			
			Replytribune m = new Replytribune();
			m.setReContent(decode(reContent));
			m.setReUserAvatar(reUserAvatar == null ? "" : reUserAvatar);
			m.setReUserName(decode(reUserName));
			m.setRuserId(Long.valueOf(reUserId == null ? "0" : reUserId));
			m.setReUserAvatar(reUserAvatar == null ? "" : reUserGender);
			m.setReUserGender(Integer.valueOf(reUserGender == null ? "" : reUserGender));
			m.setTribune(Long.valueOf(tid));
			Replytribune rt = replyTribuneService.saveAndFlush(m);
			Circle circle = circleService.findOne(rt.getTribune());
			circle.setReplyId(Long.valueOf(reUserId == null ? "0" : reUserId));
			circleService.update(circle);

			return MsgUtil.doneOk(rt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();

	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "listByTid", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findPageByTid(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize,
			@RequestParam("tid") long tid) {
		try {
			Page<Replytribune> result = replyTribuneService.getReplyListByPageAndTid(page, pageSize, tid);
			return MsgUtil.doneOk(result.getContent());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 
	 * @param value
	 * @return
	 */
	private String decode(String value) {
		try {
			String str = (value == null ? "" : value);
			return URLDecoder.decode(str, DEFAULT_ENCONDING);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}
}
